//
//  RABookMallCell.m
//  ReadApp
//
//  Created by 飞猪 on 2018/11/4.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "RABookMallCell.h"
#import <SDWebImage/SDWebImageDownloader.h>

@interface RABookMallCell ()

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIButton *morebtn;
@property (nonatomic, strong) NSMutableArray<UIImageView *> *imgs;

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIView *bodyView;

@end

@implementation RABookMallCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  if (self) {
    [self addSubViews];
  }
  return self;
}

- (void)addSubViews {
  
  self.backgroundColor = UIColorHex(eeeeee);
  self.contentView.backgroundColor = UIColorHex(eeeeee);
  self.selectionStyle = UITableViewCellSelectionStyleNone;
  
  [self.contentView addSubview:self.topView];
  [self.topView addSubview:self.titleLab];
  [self.topView addSubview:self.morebtn];
  
  [self.contentView addSubview:self.bodyView];
  for (int i = 0; i < 3; ++i) {
    UIImageView *img = [[UIImageView alloc] init];
    img.tag = i;
    img.userInteractionEnabled = YES;
    [self.imgs addObject:img];
    [img addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgClicked:)]];
    img.backgroundColor = [UIColor grayColor];
    [self.bodyView addSubview:img];
  }
  
  [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.right.left.mas_equalTo(self.contentView);
    make.height.mas_equalTo(44.f);
  }];
  
  [self.bodyView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.bottom.right.left.mas_equalTo(self.contentView);
    make.top.mas_equalTo(self.topView.mas_bottom).mas_offset(0.5);
  }];
  
  [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(10.f);
    make.centerY.mas_equalTo(0.f);
  }];
  
  [self.morebtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.mas_equalTo(-10.f);
    make.centerY.mas_equalTo(0.f);
  }];
  
  [self.imgs mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:10.f leadSpacing:10.f tailSpacing:10.f];
  CGFloat w = ([UIScreen mainScreen].bounds.size.width - 40 ) / 3;
  [self.imgs mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(10.f);
    make.height.mas_equalTo(w * 1.3);
    make.bottom.mas_equalTo(-10.f);
  }];

}

- (void)setModel:(RABookFaceModel *)model {
  _model = model;
  
  self.titleLab.text = _model.name;
  NSLog(@"_________%@", _model.name);
  
  if (_model.children && _model.children.count > 0) {
    
    for (int i = 0; i < 3; ++i) {
      UIImageView *img = [self.imgs objectAtIndex:i];
      RABookFaceModel *subModel = [_model.children objectAtIndex:i];
      [img sd_setImageWithURL:[NSURL URLWithString:subModel.iconUrl]  placeholderImage:[UIImage imageNamed:@"default_placeImage"]];
      
//      [img sd_setImageWithURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1541337502800&di=d900dd5ae16a0d72a37c988298adfa3d&imgtype=0&src=http%3A%2F%2Fpic39.photophoto.cn%2F20160604%2F0832083766346655_b.jpg"] placeholderImage:[UIImage imageNamed:@"default_placeImage"]];
    }
    
  }
  
}

#pragma mark - Event
- (void)imgClicked:(UIGestureRecognizer *)ges {
  UIView *targetV = ges.view;
  [self callback:RABookMallCellCallBackTypeDetail index:targetV.tag];
}

- (void)clicked:(UIButton *)btn {
  [self callback:RABookMallCellCallBackTypeMore index:0];
}

- (void)callback:(RABookMallCellCallBackType)type index:(NSInteger)index {
  if (self.clickedCallback) {
    self.clickedCallback(self, type, index);
  }
}

#pragma mark - Lazyload

- (UILabel *)titleLab {
  if (!_titleLab) {
    _titleLab = [UILabel new];
    _titleLab.font = [UIFont systemFontOfSize:14.f];
    _titleLab.textColor = UIColorHex(666666);
//    _titleLab.text = @"本期热推";
  }
  return _titleLab;
}

- (UIButton *)morebtn {
  if (!_morebtn) {
    _morebtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_morebtn setTitle:@"查看更多" forState:UIControlStateNormal];
    [_morebtn setTitleColor:UIColorHex(999999) forState:UIControlStateNormal];
    [_morebtn addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    _morebtn.titleLabel.font = [UIFont systemFontOfSize:12.f];
  }
  return _morebtn;
}

- (UIView *)topView {
  if (!_topView) {
    _topView = [[UIView alloc] init];
    _topView.backgroundColor = [UIColor whiteColor];
  }
  return _topView;
}

- (UIView *)bodyView {
  if (!_bodyView) {
    _bodyView = [[UIView alloc] init];
    _bodyView.backgroundColor = [UIColor whiteColor];
  }
  return _bodyView;
}


- (NSMutableArray *)imgs {
  if (!_imgs) {
    _imgs = [NSMutableArray array];
  }
  return _imgs;
}

@end
